
const app = getApp()
import { goLogin } from '../../utils/util.js'
import { getStructure, getView, getDevice } from '../../api/getData.js';
let template = require('../../pages/tabBar/tab-bar.js');
let timer
Page({
  data: {
    userName: '',
    treeRes: {},
    showNav: false,
    deatilNav: [],
    isState: app.globalData.isState,
    deatilList: [],
    deatilNavStorage: [],
    devices: false,
    mostStatus: '', // 厦门卷烟厂出现次数最多的状态字段
    sceneID: ''
  },
  onLoad: function (options) {
    template.tabbar("tabBar", 0, this);
    this.setData({
      userName: wx.getStorageSync('username')
    });
    app.watch('isState', (value) => {
      this.setData({
        isState: value,
      })
    })
    app.watch('deatilNav', (value) => {
        this.data.deatilNav = [];
        const newSet = new Set(value);
        this.setData({
          deatilNav: Array.from(newSet),
        });
    })
    app.watch('deatilList', (value) => {
      this.data.deatilList = [];
      this.setData({
        deatilList: value,
      })
    })
    app.watch('showNav', (value) => {
      this.setData({
        showNav: value,
      })
    })
    app.watch('mostStatus', (value) => {
      this.setData({
        mostStatus: value,
      })
    })
  },
  onHide(){
    console.log("onHide")

    clearInterval(timer)
  },
  onUnload(){
    this.setData({
      deatilList: [],
      deatilNav: [],
    })
    clearInterval(timer)
  },
  onShow() {
   
    wx.setStorageSync('radioChange', '1')
    clearInterval(timer)
    this.getStructureData();
    // if (wx.getStorageSync('canRequest')){
     
    // } else {
    //   let { multiList, deviceName} = app.globalData;
    //   let { devices, sceneID} = this.data;
    //   let treeName = [];
    //   multiList.forEach(item => {
    //     treeName.push(item.name)
    //   })   
    //   multiList[multiList.length - 1] && multiList[multiList.length - 1].scene && (sceneID = multiList[multiList.length - 1].scene)
    //   this.setData({
    //     deatilNav: treeName,
    //     treeName: treeName.join(',')
    //   })
    //   this.getViewData()
    // }
    wx.setStorageSync('canRequest', true)
  },
  // 获取场景树
  getStructureData(){
    getStructure().then(res => {
      if (res.status == '0' || res.resCode !== 'SUCCESS' ){
        goLogin()
        return
      }
      let result = res.result[0];
      app.globalData.result = res.result;
      if (wx.getStorageSync('deatilNav')){
        app.globalData.deatilNav = wx.getStorageSync('deatilNav')
      }else{
        // result.treeName,
        if (res.result[0].children && res.result[0].children.length) {
          app.globalData.deatilNav = [ result.children[0].treeName, (result.children[0].children && result.children[0].children[0].treeName) || ''];
        } else {
          app.globalData.deatilNav = [result.treeName, (result.children && result.children[0].treeName) || ''];
        }
        
      }  
      this.setData({
        treeRes: result,
      })
      if (wx.getStorageSync('deatilList')){
        this.setData({
          deatilList: wx.getStorageSync('deatilList'),
        })
        if (wx.getStorageSync('deatilList')[0].template) {
          app.globalData.isState = true;
        } else {
          app.globalData.isState = false;
        }
        if (wx.getStorageSync('treeInfo')) {
          this.getViewData(wx.getStorageSync('treeInfo'))
        }
      }else{
        if (res.result[0].children && res.result[0].children.length) {
          this.getViewData(result.children[0].children && result.children[0].children.length ? result.children[0].children[0] : result.children[0])
        } else {
          this.getViewData(result)
        }
      }   
    })
  },

// 获取总览信息
  getViewData (params) {
    clearInterval(timer)
    let { devices, treeID, sceneID } = params;
    // getDevice(treeID).then(res => {
    //   this.formatGetView(res)
    // })
    timer = setInterval(()=> {
      if (wx.getStorageSync('treeInfo')) {
        if (wx.getStorageSync('treeInfo').children && wx.getStorageSync('treeInfo').children.length) {
          const { devices, treeID, sceneID } = wx.getStorageSync('treeInfo');
          getView(treeID, sceneID, devices).then(res => {
            this.formatGetView(res)
          })
        } else {
          getDevice(wx.getStorageSync('treeInfo').treeID).then(res => {
            this.formatGetView(res)
          })
        }
      } else {
        getDevice(treeID).then(res => {
          this.formatGetView(res)
        })
      }
    }, 3000)
    
  },

// 处理总览数据 
  formatGetView(res,name){
    if (res.resCode == 'SUCCESS'){
      app.globalData.deatilList = res.result.view ? res.result.view : res.result;
      wx.setStorageSync('deatilList', app.globalData.deatilList)
      if (name !== undefined) {
        app.globalData.isState = true
        app.globalData.deatilNav.push(name)
        wx.setStorageSync('deatilNav', app.globalData.deatilNav)
        this.setData({
          deatilNav: app.globalData.deatilNav
        })
      }
    }
  },
// 获取下级设备列表
  getNextView(e) {
    let { treeID, treeName } = e.currentTarget.dataset.item
    // 存储treeInfo用于上面定时器请求数据
    wx.setStorageSync('treeInfo', e.currentTarget.dataset.item)
    app.globalData.treeInfo = e.currentTarget.dataset.item
    getDevice(treeID).then(res => {
      this.formatGetView(res, treeName)
    })
  },
  // 隐藏导航
  closeNav(){
    this.setData({
      showNav: false
    })
  },

  // 显示导航
  showNavFn(){
    this.setData({
      showNav: true
    })
  },

  // 跳转到详情
  goDeviceDetail ( e ){
    let { deviceid, index, name, t} = e.currentTarget.dataset,
    { deatilList} = this.data,
    multiList = [];
    if (t.moduleList.length) {
      wx.setStorageSync('moduleList_new', t.moduleList)
      wx.setStorageSync('moduleConfig_new', t.moduleConfig)
    }else{
      wx.setStorageSync('moduleList_new', [])
      wx.setStorageSync('moduleConfig_new', [])
    }
    app.globalData.deviceList = deatilList
    app.globalData.multiList = multiList
    app.globalData.deviceName = {
      deviceID: deviceid,
      deviceName: name
    };
    wx.navigateTo({
      url: `/pages/deviceDetail/deviceDetail?deviceid=${deviceid}`,
    });
  },

})